package com.kzd.btgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.entity.PlatformData
import com.kzd.btgame.entity.PlatformMoney
import razerdp.basepopup.BasePopupWindow
import java.lang.StringBuilder


class PlatformBottomPopup(
    context: Context, val onMoney: (isHave: Boolean, platformMoney: PlatformMoney) -> Unit,
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val mMoneyAdapter =
        object : BaseQuickAdapter<PlatformMoney, BaseViewHolder>(R.layout.item_platform_list) {


            var selectPosition = 0
                set(value) {
                    if (field != value) {
                        field = value
                        notifyDataSetChanged()
                    }
                }

            override fun convert(holder: BaseViewHolder, item: PlatformMoney) {
                holder.setText(
                    R.id.tvPlatformListMoney,
                    StringBuilder().append(item.money).append("CNY")
                )
                holder.itemView.isSelected = selectPosition == holder.layoutPosition
            }
        }

    private var mIsHave: Boolean = false

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_platform_bottom)
    }

    private var mPlatformMoney: PlatformMoney? = null

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivPlatformBottomClose)
            .setOnClickAntiShakeListener {
                dismiss()
            }
        contentView.findViewById<EditText>(R.id.etPlatformBottomMoney)
            .addTextChangedListener {
                if (it.isNullOrEmpty()) {
                    return@addTextChangedListener
                }
                mPlatformMoney = PlatformMoney(
                    id = 0, money = it.toString().toInt(), coin = 0
                )
            }
        contentView.findViewById<TextView>(R.id.tvPlatformBottomSubmit)
            .setOnClickAntiShakeListener {
                mPlatformMoney?.let {
                    onMoney(mIsHave, it)
                }
            }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun fillData(payType: Int, platformMoney: MutableList<PlatformMoney>): PlatformBottomPopup {
        contentView.findViewById<TextView>(R.id.tvPlatformBottomTitle)
            .text = if (payType == 2) {
            "微信支付"
        } else "支付宝支付"
        mMoneyAdapter.selectPosition = 0
        contentView.findViewById<EditText>(R.id.etPlatformBottomMoney)
            .text = null
        contentView.findViewById<TextView>(R.id.tvPlatformBottomMoney)
            .text = StringBuilder().append(platformMoney[0].money)
            .append("\tCNY\t=\t").append(platformMoney[0].coin).append("\t平台币")
        mPlatformMoney = platformMoney[0]
        contentView.findViewById<RecyclerView>(R.id.rvPlatformBottomBody)
            .adapter = mMoneyAdapter.apply {
            setNewInstance(platformMoney)
        }
        mMoneyAdapter.setOnItemClickListener { _, _, position ->
            mMoneyAdapter.selectPosition = position
            val itemData = mMoneyAdapter.getItem(position)
            contentView.findViewById<TextView>(R.id.tvPlatformBottomMoney)
                .text = StringBuilder().append(itemData.money)
                .append("\tCNY\t=\t").append(itemData.coin).append("\t平台币")
            contentView.findViewById<EditText>(R.id.etPlatformBottomMoney)
                .text = null
            mPlatformMoney = itemData
        }
        return this
    }

    fun fillData(payType: Int, platformData: PlatformData): PlatformBottomPopup {
        val platformMoney: MutableList<PlatformMoney> = platformData.data
        if (platformMoney.isNullOrEmpty()) {
            return this
        }
        val cbPlatformBottomDoubleCard =
            contentView.findViewById<CheckBox>(R.id.cbPlatformBottomDoubleCard)
                .apply {
                    visibility = if (platformData.is_have > 0) {
                        View.VISIBLE
                    } else View.GONE
                    isChecked = false
                }
        contentView.findViewById<TextView>(R.id.tvPlatformBottomTitle)
            .text = if (payType == 2) {
            "微信支付"
        } else "支付宝支付"
        mMoneyAdapter.selectPosition = 0
        contentView.findViewById<EditText>(R.id.etPlatformBottomMoney)
            .text = null
        contentView.findViewById<TextView>(R.id.tvPlatformBottomMoney)
            .text = StringBuilder().append(platformMoney[0].money)
            .append("\tCNY\t=\t").append(platformMoney[0].coin).append("\t平台币")
        mPlatformMoney = platformMoney[0]
        contentView.findViewById<RecyclerView>(R.id.rvPlatformBottomBody)
            .adapter = mMoneyAdapter.apply {
            setNewInstance(platformMoney)
        }
        mMoneyAdapter.setOnItemClickListener { _, _, position ->
            mMoneyAdapter.selectPosition = position
            val itemData = mMoneyAdapter.getItem(position)
            val coin = if (cbPlatformBottomDoubleCard.isChecked && platformData.is_have > 0)
                itemData.coin * platformData.is_have else itemData.coin
            contentView.findViewById<TextView>(R.id.tvPlatformBottomMoney)
                .text = StringBuilder().append(itemData.money)
                .append("\tCNY\t=\t").append(coin).append("\t平台币")
            contentView.findViewById<EditText>(R.id.etPlatformBottomMoney)
                .text = null
            mPlatformMoney = itemData
        }
        cbPlatformBottomDoubleCard.setOnCheckedChangeListener { _, isChecked ->
            val itemData = mMoneyAdapter.getItem(mMoneyAdapter.selectPosition)
            val coin = if (isChecked)
                itemData.coin * platformData.is_have else itemData.coin
            contentView.findViewById<TextView>(R.id.tvPlatformBottomMoney)
                .text = StringBuilder().append(itemData.money)
                .append("\tCNY\t=\t").append(coin).append("\t平台币")
            this.mIsHave = isChecked
        }
        return this
    }

}